package com.finstone.data.spider.eshop.pipeline;

import java.io.File;

import org.springframework.stereotype.Component;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.utils.FilePersistentBase;

import com.finstone.data.spider.utils.ImageBase64Utils;

@Component
public class EShopGoodsDetailImagePipline extends FilePersistentBase implements Pipeline {

	public static final String IMAGE_SAVEAS = "d:/eshop/goods/";

	public EShopGoodsDetailImagePipline() {
		setPath(IMAGE_SAVEAS);
	}

	public EShopGoodsDetailImagePipline(String path) {
		setPath(path);
	}

	public void process(ResultItems resultItems, Task task) {

		String imageBinStr = resultItems.get("imageBinStr");
		String id = resultItems.get("id");
		String dir_id = resultItems.get("dir_id");
		String suffix = resultItems.get("suffix");
		String real_id = resultItems.get("real_id");
		if (suffix.length() > 8) {
			suffix = ".jpg";
		}
		String path = this.path + File.separator + real_id + File.separator;
		checkAndMakeParentDirecotry(path);
		ImageBase64Utils.generateImage(path, id + suffix, imageBinStr);
	}
}
